/*******************************************************************************
 Jimm - Mobile Messaging - J2ME ICQ clone
 Copyright (C) 2003-05  Jimm Project

 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; either version 2
 of the License, or (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 ********************************************************************************
 File: src/jimm/comm/FromIcqSrvPacket.java
 Version: ###VERSION###  Date: ###DATE###
 Author(s): Manuel Linsmayer, Andreas Rossbacher
 *******************************************************************************/


// #sijapp cond.if protocols_ICQ is "true" #
package protocol.icq.packet;


import jimm.*;
import jimm.comm.*;
import protocol.icq.*;


public class FromIcqSrvPacket extends SnacPacket {


	// SRV_OFFLINEMSG packet subcommand
	public static final int SRV_OFFLINEMSG_SUBCMD = 0x0041;


	// SRV_DONEOFFLINEMSGS packet subcommand
	public static final int SRV_DONEOFFLINEMSGS_SUBCMD = 0x0042;


	// SRV_META packet subcommand and types
	public static final int SRV_META_SUBCMD = 0x07DA;
	public static final int SRV_META_GENERAL_TYPE = 0x00C8;
	public static final int META_SET_FULLINFO_ACK = 0x0C3F;

	/****************************************************************************/
	/****************************************************************************/
	/****************************************************************************/

	// ICQ sequence number
	protected int icqSequence;

	// UIN
	protected String uin;

	// Subcommand
	protected int subcommand;


	// Constructor
	public FromIcqSrvPacket(long reference, int snacFlags, int icqSequence, String uin, int subcommand, byte[] extData, byte[] data) {
		super(SnacPacket.OLD_ICQ_FAMILY, SnacPacket.SRV_FROMICQSRV_COMMAND, snacFlags, reference, extData, data);
		this.icqSequence = icqSequence;
		this.uin = uin;
		this.subcommand = subcommand;
	}


	// Returns the ICQ sequence number
	public final int getIcqSequence() {
		return this.icqSequence;
	}


	// Sets the ICQ sequence number
	public final void setIcqSequence(int icqSequence) {
		this.icqSequence = icqSequence;
	}

	// Returns the subcommand
	public final int getSubcommand() {
		return this.subcommand;
	}


	// Returns the package as byte array
	public byte[] toByteArray() {
        // #sijapp cond.if modules_DEBUGLOG is "true" #
        jimm.modules.DebugLog.panic("Unsupported operation (FromIcqSrvPacket.toByteArray()");
        // #sijapp cond.end #
        return null;
	}


	// Parses given byte array and returns a FromIcqSrvPacket object
	public static Packet parse(byte[] flapData) throws JimmException {
		// Get length of FLAP data
		int flapLength = flapData.length;

		// Check length (min. 24 bytes)
		if (flapLength < 24) {
			throw new JimmException(137, 0);
		}

		// Get SNAC flags
		int snacFlags = Util.getWordBE(flapData, 4);

		// Get SNAC reference
		long snacReference = Util.getDWordBE(flapData, 6);

		// Get data and extra data (if available)
		byte[] extData;
		String uin;
		int subcommand;
		int icqSequence;
        int extDataDelta = 0;
		if (snacFlags == 0x8000) {

			// Check length (min. 26 bytes)
			if (flapLength < 24 + 2) {
				throw new JimmException(137, 1);
			}

			// Get length of extra data
			int extDataLength = Util.getWordBE(flapData, 10);

			// Check length (min. 26+extDataLength bytes)
			if (flapLength < 24 + 2 + extDataLength) {
				throw new JimmException(137, 2);
			}
			// Get extra data
			extData = new byte[extDataLength];
			System.arraycopy(flapData, 10 + 2, extData, 0, extDataLength);

            extDataDelta = 2 + extDataLength;

        } else {
            extData = emptyArray;
        }

        // Get uin, subcommand and icq sequence number
        uin = String.valueOf(Util.getDWordLE(flapData, 10 + 6 + extDataDelta));
        subcommand = Util.getWordLE(flapData, 10 + 10 + extDataDelta);
        icqSequence = Util.getWordLE(flapData, 10 + 12 + extDataDelta);
        
        // Get data
        byte[] data = new byte[flapLength - 10 - 14 - extDataDelta];
        System.arraycopy(flapData, 10 + 14 + extDataDelta, data, 0, data.length);

		// Instantiate FromIcqSrvPacket
		return new FromIcqSrvPacket(snacReference, snacFlags, icqSequence, uin, subcommand, extData, data);

	}
}
// #sijapp cond.end #
